package com.doge.walknovel.business.novel.chapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.doge.wnpro.R
import com.doge.walknovel.api.model.ChapterInfo
import com.doge.walknovel.core.base.adapters.AdapterCallback
import com.doge.walknovel.core.base.adapters.ITEM_CLICKED
import com.doge.wnpro.databinding.CellNovelChapterBinding
import com.doge.walknovel.utils.html
import com.doge.walknovel.utils.setOnClickCallback

class NovelChapterViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    private val binding: CellNovelChapterBinding =
        CellNovelChapterBinding.inflate(LayoutInflater.from(parent.context), parent, false),
) : RecyclerView.ViewHolder(binding.root) {
    init {
        itemView.setOnClickCallback {
            callback.invoke(ITEM_CLICKED, itemView.tag)
        }
    }

    fun bind(item: ChapterInfo) {
        itemView.tag = item
        binding.chapterName.text = item.chapterTitle.html
        binding.chapterStatus.isVisible = !item.free
        binding.chapterStatus.setImageResource(
            if (item.locked) R.mipmap.novel_chapter_lock
            else R.mipmap.novel_chapter_unlock
        )
    }
}